package com.hghivln.controller.doctor;

import com.hghivln.aspectj.annotation.Log;
import com.hghivln.aspectj.enums.BusinessType;
import com.hghivln.pojo.dto.SchedulingFormDto;
import com.hghivln.pojo.dto.SchedulingQueryDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.SchedulingService;
import com.hghivln.utils.ShiroUtils;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("doctor/scheduling")
public class SchedulingController {

    @Reference
    private SchedulingService schedulingService;

    /**
     *  查询需要排班的医生列表 可以传部门ID 如果部门ID为空，则直接显示所有
     */
    @GetMapping("queryUsersNeedScheduling")
    @HystrixCommand
    public AjaxResult queryUsersNeedScheduling(Long deptId) {
        SchedulingQueryDto schedulingQueryDto = new SchedulingQueryDto();
        schedulingQueryDto.setDeptId(deptId);
        return AjaxResult.success(schedulingService.selectUsersNeedScheduling(schedulingQueryDto));
    }

    /**
     * 查询排班医生的排班信息
     */
    @GetMapping("queryScheduling")
    @HystrixCommand
    public AjaxResult queryScheduling(SchedulingQueryDto schedulingQueryDto) {
        return schedulingService.queryUsersNeedScheduling(schedulingQueryDto);
    }

    /**
     * 保存排班数据
     */
    @PostMapping("saveScheduling")
    @Log(title = "添加排班数据",businessType = BusinessType.INSERT)
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "6000")
    })
    public AjaxResult saveScheduling(@RequestBody @Validated SchedulingFormDto schedulingFormDto) {
        schedulingFormDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return schedulingService.saveScheduling(schedulingFormDto);
    }

    /**
     * 查询当前登录用户的排班信息
     */
    @GetMapping("queryMyScheduling")
    @HystrixCommand
    public AjaxResult queryMyScheduling(SchedulingQueryDto schedulingQueryDto) {
        // 根据部门ID 用户ID 查询用户信息，如果部门ID 和 用户ID 都为空，那么就查询所有要排班的用户信息
        schedulingQueryDto.setUserId((Long) ShiroUtils.getCurrentSimpleUser().getUserId());
        return schedulingService.queryUsersNeedScheduling(schedulingQueryDto);
    }

}
